<?php
/**
 * Category Hider by WallPhone
 */
function Category_HideFromDiscussions(&$DiscussionManager) {
    $SB = &$DiscussionManager->DelegateParameters['SqlBuilder'];
    $SB->AddWhere('t', 'CategoryID', '', (int)$DiscussionManager->Context->Configuration['BLOG_CATEGORY_ID'], '<>', 'and', '', 0, 0);
}
/**
 * Redirect to Blog page after discussion is saved, and Blog category was selected
 */
function DiscussionForm_BlogRedirect(&$DiscussionForm) {
    if ($DiscussionForm->Context->WarningCollector->Iif() && $DiscussionForm->Discussion->CategoryID == $DiscussionForm->Context->Configuration['BLOG_CATEGORY_ID']) {
        $Url = GetUrl($DiscussionForm->Context->Configuration, 'extension.php', 'category/', 'CategoryID', $Context->Configuration['BLOG_CATEGORY_ID'], '', 'PostBackAction=Blog', '' );
        header('Location: '. str_replace('&amp;', '&', $Url));
        $Context->unload();
        die();
    }
}
function DiscussionForm_RenderDraft (&$DiscussionForm) {
    global $Context;
    if ($DiscussionForm->Discussion->DiscussionID > 0) {
        $sql = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'SqlBuilder');
        $sql->SetMainTable('Discussion','d');
        $sql->AddSelect('Draft', 'd');
        $sql->AddWhere('d','DiscussionID','',$DiscussionForm->DiscussionID,'=');
        $result = $Context->Database->Select($sql, $DiscussionForm, 'Render_Draft', 'An error occurred while retrieving Drafts field.');
        $rows = $Context->Database->GetRow($result);
        $Draft = $rows["Draft"];
    }
    else $Draft = 0;
    echo '<li class="Draft">
          <label>'.GetBasicCheckBox("Draft", 1, $Draft, '').'  '.$Context->GetDefinition('Draft').'</label>
          </li>   ';
}

function DiscussionForm_SaveDraft (&$DiscussionForm) {
    $Draft = ForceIncomingInt('Draft', 0);
    $ResultDiscussion = $DiscussionForm->DelegateParameters['ResultDiscussion'];
    if ($ResultDiscussion) {
        $sql = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'SqlBuilder');
        $sql->SetMainTable('Discussion', 'd');
        $sql->AddFieldNameValue('Draft', $Draft);
        $sql->AddWhere('d', 'DiscussionID', '', $ResultDiscussion->DiscussionID, '=');
        $DiscussionForm->Context->Database->Update($sql, $DiscussionForm, 'DiscussionForm_Draft', 'An error occurred while updating discussion properties.');
    }
}
function DiscussionManager_ExcludeDraft(&$DiscussionManager) {
    $s = &$DiscussionManager->DelegateParameters['SqlBuilder'];
    $s->AddWhere('t', 'Draft', '', '0', '=');
}

function DiscussionManager_ShowDraft(&$DiscussionManager) {
    $s = &$DiscussionManager->DelegateParameters['SqlBuilder'];
    $s->AddWhere('t', 'Draft', '', '1', '=');
}



/**
 * Get main/extended entry (<!--more-->)
 * from wordpress post.php
 */
function get_main($post) {
    //Match the new style more links
    if ( preg_match('/<!--more(.*?)-->/', $post, $matches) ) {
	list($main, $extended) = explode($matches[0], $post, 2);
    } else {
	$main = $post;
    	$extended = '';
    }

    // Strip leading and trailing whitespace
    //$main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
    //$main = nl2br($main);

    //$extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
    return array('main' => $main, 'extended' => $extended);
}

function ChangeCurrentTab (&$Menu){
    $Menu->CurrentTab = 'Blog';
}
/*
function AddLatestBlogPostToPanel (&$Context, &$Panel, &$DiscussionManager) {
    global $Panel;
    $LatestBlogPostList = "";
    $Discussion = $Context->ObjectFactory->NewContextObject($Context, "Discussion");
    $LatestBlogPost = $DiscussionManager->GetDiscussionList(5, 1, $Context->Configuration["BLOG_CATEGORY_ID"]);

    while ($Row = $DiscussionManager->Context->Database->GetRow($LatestBlogPost)) {
        $Discussion->Clear();
        $Discussion->GetPropertiesFromDataSet($Row, $Context->Configuration);
        $Discussion->FormatPropertiesForDisplay();
        $LatestBlogPostList .= '<li><a href="'.GetUrl($Context->Configuration, "comments.php", "", "DiscussionID", $Discussion->DiscussionID, "", "#Item_1", CleanupString($Discussion->Name)."/").'">'.$Discussion->Name.'</a></li>';

    }
    $LatestBlogPostList .= '<li><a href="'.GetUrl($Context->Configuration, "extension.php", "category/", "CategoryID", $Context->Configuration["BLOG_CATEGORY_ID"], "",  "PostBackAction=Blog").'">Show all</a></li>';
    return $LatestBlogPostList;
}*/
?>
